import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
import dayjs from "dayjs";

export interface FlightIndexState {
  dptCityId: number;
  dptCityName: string;
  dptAirportName: string;
  arrCityId: number;
  arrCityName: string;
  arrAirportName: string;
  cityType: string; // 选择的城市类型 depart: 出发， arrive：到达
  dptDate: string; // 起飞时间
}

const initialState: FlightIndexState = {
  dptCityId: 2,
  dptCityName: "上海",
  dptAirportName: "虹桥机场",
  arrCityId: 1,
  arrCityName: "北京",
  arrAirportName: "大兴机场",
  cityType: "depart", // 选择的城市类型 depart: 出发， arrive：到达
  dptDate: dayjs().format("YYYY-MM-DD"), // 起飞时间
};

export const FlightIndexSlice = createSlice({
  name: "flightIndex",
  initialState,
  reducers: {
    updateState: (state, action: PayloadAction<Partial<FlightIndexState>>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
});

// Action creators are generated for each case reducer function
export const { updateState } = FlightIndexSlice.actions;

export default FlightIndexSlice.reducer;
